"""
PROJECT: django_celery_demo
FILE: serializers.py
AUTHOR: wensen
CREATE: 2022-03-04 
"""
from drf_haystack.serializers import HaystackSerializer
from rest_framework import serializers

from haystack_demo.models import Goods
from haystack_demo.search_indexes import GoodsIndex


class GoodsSerialize(serializers.ModelSerializer):
    class Meta:
        model = Goods
        fields = "__All__"


class SearchSerialize(HaystackSerializer):

    def update(self, instance, validated_data):
        pass

    def create(self, validated_data):
        pass

    class Meta:
        index_classes = [GoodsIndex]
        fields = ("goods_id", 'goods_name', 'goods_description', "create_time", "price", "inventory")

    def to_representation(self, instance):
        instance.create_time = instance.create_time.strftime("%Y-%m-%d %H:%M:%S")
        return super().to_representation(instance)
